<?php
namespace modules\user;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;
use \phiction\exceptions\bad_request;

class api
{
    static function edit_my_password($q, &$args)
    {
        $old_password = arr::get_or_bad_request($args, 'old-password');
        $new_password = arr::get_or_bad_request($args, 'new-password');

        $user = auth::validate_with_password($q, $args, $old_password);
        $id   = $user['id'];
        $org  = $user['org'];

        if (storage::reset_password($q, $org, $id, $new_password)->run() !== 1)
            throw new bad_request("修改密码失败");

        auth::logout($q, $args);
    }

    static function edit_password($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');
        $new_password = arr::get_or_bad_request($args, 'new-password');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        if ($user['id'] === $id)
            throw new bad_request("不能在此修改");

        if (storage::reset_password($q, $org, $id, $new_password)->run() !== 1)
            throw new bad_request("修改密码失败");
    }

    static function leave($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        if ($user['id'] === $id)
            throw new bad_request("不能离职自己");

        auth::revoke_token($q, $args);

        if (storage::active($q, $org, $id, false)->run() !== 1)
            throw new bad_request("用户离职失败");
    }

    static function revive($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        auth::revoke_token($q, $args);

        if (storage::active($q, $org, $id, true)->run() !== 1)
            throw new bad_request("用户复职失败");
    }

    static function my_profile($q, &$args)
    {
        $user = auth::validate($q, $args);
        $org  = $user['org'];
        $id   = $user['id'];

        $profile = storage::profile($q, $org, $id)->run();
        if (is_null($profile))
            throw new bad_request("查看用户资料失败");

        return $profile;
    }

    static function profile($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        $profile = storage::profile($q, $org, $id)->run();
        if (is_null($profile))
            throw new bad_request("无此用户");

        return $profile;
    }

    static function list_active($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, true)->run();
        return $list;
    }

    static function list_archived($q, &$args)
    {
        $page = (int)arr::get_or_bad_request($args, 'page');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        $list = storage::list_by_active($q, $org, $page, false)->run();
        return $list;
    }

    static function add($q, &$args)
    {
        $name     = arr::get_or_bad_request($args,     'name');
        $role     = arr::get_or_bad_request($args,     'role');
        $username = arr::get_or_bad_request($args, 'username');
        $password = arr::get_or_bad_request($args, 'password');
        $comment  = arr::get_or($args, 'comment', '');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        $id = storage::id_from_username($q, $org, $username)->run();
        if (!is_null($id)) throw new bad_request("用户名已存在");

        if (storage::add($q,
                $username, $password, $role,
                $name, $comment, $org)->run() !== 1)
            throw new bad_request("添加用户失败");
    }
    static function edit_role($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');
        $new_role = arr::get_or_bad_request($args, 'role');

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        if ($user['id'] === $id)
            throw new bad_request("不能修改自己的角色");

        if (storage::reset_role($q, $org, $id, $new_role)->run() !== 1)
            throw new bad_request("修改密码失败");
    }

    static function edit_others($q, &$args)
    {
        $id = (int)arr::get_or_bad_request($args, 'id');
        $data = [
            'name'    => arr::get_or_bad_request($args, 'name'),
            'comment' => arr::get_or_bad_request($args, 'comment'),
        ];

        $user = auth::validate_with_role($q, $args, 'admin');
        $org  = $user['org'];

        if (storage::others($q, $org, $id, $data)->run() !== 1)
            throw new bad_request("修改信息失败");
    }
}

